package com.quasar.hpatient.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.quasar.hpatient.R;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.medicalmodel.DoctorListBean;
import com.quasar.hpatient.onInterface.OnDoctorListener;
import com.quasar.hpatient.presenter.DoctorFindPresenter;
import com.quasar.hpatient.utils.LogAndToastUtil;
import com.quasar.hpatient.view.adapter.ChoiceDoctorxAdapter;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * author : 许建宏
 * Date: 2017/12/15 0015
 * View:
 */

public class ChoiceDoctorxDialog extends Dialog implements OnDoctorListener {

    Context context;

    RecyclerView rv_list;
    private ChoiceDoctorxAdapter choiceDoctorxAdapter;
    int page = 1;
    TextView tv_hospital;
    TextView tv_department;
    ImageView iv_left;
    TextView tv_title;
    private DoctorFindPresenter doctorFindPresenter;

    public ChoiceDoctorxDialog(Context context) {
        this(context, R.style.dialog_common);
        this.context = context;
    }

    @SuppressLint("InflateParams")
    public ChoiceDoctorxDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);

        View contentView = getLayoutInflater().inflate(
                R.layout.choice_doctorx_layout, null);

        rv_list = (RecyclerView) contentView.findViewById(R.id.rv_list);
        tv_hospital = (TextView) contentView.findViewById(R.id.tv_hospital);
        tv_department = (TextView) contentView.findViewById(R.id.tv_department);
        iv_left = (ImageView) contentView.findViewById(R.id.iv_left);
        tv_title = (TextView) contentView.findViewById(R.id.tv_title);
        iv_left.setVisibility(View.INVISIBLE);
        tv_title.setText("选择提交诊断医生");
        tv_hospital.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消
                ChoiceDoctorxDialog.this.dismiss();
                listener.DialogDetermine(null);

            }
        });
        tv_department.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //确定
                List<DoctorListBean> doctorListBeans = new ArrayList<>();

                for (int i = 0; i < choiceDoctorxAdapter.getData().size(); i++) {
                    if (choiceDoctorxAdapter.getData().get(i).isChoice()) {
                        ViseLog.d(choiceDoctorxAdapter.getData().get(i).getRealName());
                        doctorListBeans.add(choiceDoctorxAdapter.getData().get(i));
                    }
                }
                listener.DialogDetermine(doctorListBeans);
                ChoiceDoctorxDialog.this.dismiss();
            }
        });
        rv_list.setLayoutManager(new LinearLayoutManager(context));
        initAdapter();

        super.setContentView(contentView);
    }

    public void initAdapter() {
        doctorFindPresenter = new DoctorFindPresenter(this);
        choiceDoctorxAdapter = new ChoiceDoctorxAdapter();
        rv_list.setAdapter(choiceDoctorxAdapter);
        doctorFindPresenter.MyDoctorxData(page + "");
    }

    @Override
    public void onDoctorListSuccess(AnotherResult<DoctorListBean> data) {

    }

    @Override
    public void onMyDoctorSuccess(final AnotherResult<DoctorListBean> data) {
        if(data.getList()!=null && data.getList().size()>0){
            choiceDoctorxAdapter.setNewData(data.getList());
        }
        listener.Mydoctorx(data);
    }

    @Override
    public void onError(String msg) {
        if (msg != null) {
            LogAndToastUtil.toast(context, msg, "");
        }
    }

    @Override
    public void onRefreshUI() {

    }

    @Override
    public void onSubmitSuccess(AnotherResult<DoctorListBean> dataStateResult) {

    }

    public void setListener(OnCustomDialogDetermineListener listener) {
        this.listener = listener;
    }

    public OnCustomDialogDetermineListener listener;

    public interface OnCustomDialogDetermineListener {
        void DialogDetermine(List<DoctorListBean> data);
        void Mydoctorx(AnotherResult<DoctorListBean> anotherResult);
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (!hasFocus) {
            return;
        }
        setHeight();
    }

    private void setHeight() {
        Window window = getWindow();
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        WindowManager.LayoutParams attributes = window.getAttributes();
        if (window.getDecorView().getHeight() >= (int) (displayMetrics.heightPixels * 0.6)) {
            attributes.height = (int) (displayMetrics.heightPixels * 0.65);
        }
        window.setAttributes(attributes);
    }

    @Override
    public void onDeleteDoctorx(DataStateResult dataStateResult) {

    }

    @Override
    public void onApplyDoctorx(DataStateResult dataStateResult) {

    }
}
